$(function(){
	
	document.documentElement.style.fontSize = window.innerWidth/3.75 + 'px';
	
	var tel,code;
	var shareKey = getQueryStr('shareKey');
	var activityId = getQueryStr('activityId');
	
	//获取文字说明
    $.ajax({
		type: "post",
		url: "/dingding-web/comActAction!getShareIndexInfo.action",
		dataType: "jsonp",
		async: false,
		jsonp: "callback",
		jsonpCallback: "success_jsop",
		data: {
			activityId : activityId,
			userId : "N/A"
		},
		success: function(data) {
			var bannerImage = data.responseBody.bannerImage;
			var html = data.responseBody.rule;
			$("#activityImg").attr("src",bannerImage);
			$("#activityInfo").html(html);
		}
    });
	
	
	$('.getCode-btn').on('click',function(){
		
		$(".getCode-btn").attr("disabled", true);
		
		var re = /^1[3|4|5|7|8][0-9]{9}$/;
        var tel_click = $(".tel-input").val();
        tel = tel_click;
        
        if(re.test(tel)){
        	//获取验证码
		    $.ajax({
				type: "post",
				url: "/dingding-web/comActAction!getTelePhoneCode.action",
				dataType: "jsonp",
				async: false,
				jsonp: "callback",
				jsonpCallback: "success_jsop",
				data: {
					telephone: tel
				},
				success: function(data) {
					if(data.code != 200) {
						alert(data.errorMessage);
						$(".getCode-btn").attr("disabled", false);
					}
					else {
						//一分钟之后才能继续获取
			        	var count = 60;
					    var countdown = setInterval(CountDown, 1000);
					    function CountDown() {
					        $(".getCode-btn").attr("disabled", true);
					        $(".getCode-btn").css({'background-color':'#939090'});
					        $(".getCode-btn").text(count + "s");
					        if (count == 0) {
					            $(".getCode-btn").text("获取验证码").removeAttr("disabled");
					            $(".getCode-btn").css({'background-color':'#FF4B4A'});
					            clearInterval(countdown);
					        }
					        count--;
					    }
					}
				}
			});
        }
        else {
        	alert('请输入正确的号码格式');
        	$(".getCode-btn").attr("disabled", false);
        }
		
	});
	
	//点击注册
	$('.register-btn').on('click',function(){
		tel = $('.tel-input').val();
		code = $('.code-input').val();
		imgCode = $('.imgcode-input').val();
		if(tel && code && imgCode) {
			$.ajax({
				type: "post",
				url: "/dingding-web/comActAction!regUserCouponActi.action",
				dataType: "jsonp",
				async: false,
				jsonp: "callback",
				jsonpCallback: "success_jsop",
				data: {
					activityId : activityId,
					telephone: tel,
					phoneCode: code,
					shareKey: shareKey,
					graphCode: imgCode
				},
				success: function(data) {
					if(data.code != 200) {
						alert(data.errorMessage);
					}
					else {
						
						//优惠券领完的，活动结束
						if(data.responseBody.userType == 1) {
							window.location.href = '/dingding-web/laxin/successRegister.html?status=1';
						}
						
						//没领取过优惠券的
						if(data.responseBody.userType == 2) {
							window.location.href = '/dingding-web/laxin/successRegister.html?status=2';
						}
						
						//领取过优惠券的
						if(data.responseBody.userType == 3) {
							window.location.href = '/dingding-web/laxin/successRegister.html?status=3';
						}
						
						//活动已结束
						if(data.responseBody.userType == 4) {
							window.location.href = '/dingding-web/laxin/successRegister.html?status=4';
						}
						
						//领取以优惠券异常
						if(data.responseBody.userType == 5) {
							window.location.href = '/dingding-web/laxin/successRegister.html?status=5';
						}
						
					}
				}
			});
		}
		else {
			alert('请输入手机号码和验证码');
		}
	});
	
	//点击图片重新获取图片验证码
	$('.imgcode-div img').on('click',function (){
		$(this).attr('src','/dingding-web/validateCode?'+Math.random());
	});
	
})